package com.regex;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author WaveHi
 * @Date 12/8/2021 上午 9:12
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description 求员工的平均薪资
 */
public class SalaryAvg {

    public static void main(String[] args) throws ParseException {

        //获取数据源
        String text = "name=小红&age=17&salary=8000.00&hireDate=1991-07-03;"
                + "name=小明&age=23&salary=10000.00&hireDate=1992-12-11;"
                + "name=小白&age=18&salary=12000.00&hireDate=1996-01-09";

        //接收数据
        String[] ems = text.split(";");
        String[] names = null;
        String[] ages = null;
        String[] salarys = null;
        String[] hireDates = null;
        String name = null;
        int age = 0;
        BigDecimal salary = null;
        Date hireDate = null;

        //接收对象
        int count = 0;
        Employee[] employees = new Employee[3];

        //提取数据
        for(String em : ems) {

            //拆分属性
            String[] attributes = em.split("&");
            names = attributes[0].split("=");
            ages = attributes[1].split("=");
            salarys = attributes[2].split("=");
            hireDates = attributes[3].split("=");

            //需要日期对象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            //获取员工
            employees[count] = new Employee(names[1],Integer.parseInt(ages[1]),new BigDecimal(salarys[1]),sdf.parse(hireDates[1]));
            count++;


        }

        //计算平均薪资
        BigDecimal sum = new BigDecimal("0.0");
        for(Employee e : employees) {

            sum = sum.add(e.getSalary());

        }
        BigDecimal avg = sum.subtract(new BigDecimal("" + (count + 1)));
        System.out.println(avg);

    }

}

//员工类
class Employee {

    private String name;//姓名
    private int age;//年龄
    private BigDecimal salary;//薪资
    private Date hireDate;//入职时间

    public Employee() {

    }

    public Employee(String name, int age, BigDecimal salary, Date hireDate) {

        this.name = name;
        this.age =  age;
        this.salary = salary;
        this.hireDate = hireDate;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getName() {

        return name;

    }

    public void setAge(int age) {

        this.age = age;

    }

    public int getAge() {

        return age;

    }

    public void setSalary(BigDecimal salary) {

        this.salary = salary;

    }

    public BigDecimal getSalary() {

        return salary;

    }

    public void setHireDate(Date hireDate) {

        this.hireDate = hireDate;

    }

    public Date getHireDate() {

        return hireDate;

    }

}
